package com.cos.blog.services.impl;

import com.cos.blog.core.dao.IBaseDao;
import com.cos.blog.core.service.BaseServiceImpl;
import com.cos.blog.core.utils.PasswordUtils;
import com.cos.blog.core.utils.UUIDUtils;
import com.cos.blog.dao.IUserDao;
import com.cos.blog.dao.impl.UserDaoImpl;
import com.cos.blog.entity.UserEntity;
import com.cos.blog.services.IUserService;

import java.util.Date;

public class UserServiceImpl extends BaseServiceImpl<UserEntity> implements IUserService {

    private IUserDao userDao = new UserDaoImpl();

    @Override
    protected IBaseDao<UserEntity> getBaseDao() {
        return userDao;
    }

    /**
     * @Author:zxc
     * @Description: 登录
     */
    @Override
    public UserEntity login(String username, String password) throws Exception {
        // 1.根据用户用户名查询
        UserEntity userEntity = userDao.selectByUsername(username);
        if (userEntity == null) {
            // 用户名不存在
            return null;
        }
        // 2、密码的比对
        if (!PasswordUtils.check(password, userEntity.getPassword())) {
            return null;
        }
        return userEntity;
    }

    /**
     * @Author:zxc
     * @Description: 注册
     */
    @Override
    public void register(UserEntity userEntity) throws Exception {
        // 2.密码加密
        userEntity.setPassword(PasswordUtils.hashPw(userEntity.getPassword()));

        // 3.设置用户的状态
        //未激活
        userEntity.setStatus(1);
        userEntity.setCreateTime(new Date());
        // 激活码
        userEntity.setActivationCode(UUIDUtils.getId());
        userEntity.setType(0);

        // 4.入库
        int save = userDao.insert(userEntity);
    }

    @Override
    public UserEntity byActivationCode(String activationCode) throws Exception {
        return userDao.selectByActivationCode(activationCode);
    }

    @Override
    public void updateHeaderUrl(Integer id, String filePath) throws Exception {
        userDao.updateHeaderUrl(id,filePath);
    }
}
